#!/usr/bin/make -f
# -*- makefile -*-
# Makefile for MRPT Debian package.

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,+fortify

# default: dont build expensive caller graphs with dot in the documentation pkg
export MRPT_genCALLERGRAPHS=NO

# For multiarch support:
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

# SSE options
ifeq ($(DEB_HOST_ARCH),amd64)
CMAKE_ARCH_FLAGS = -DMRPT_AUTODETECT_SSE=OFF -DDISABLE_SSE2=OFF -DDISABLE_SSE3=ON -DDISABLE_SSE4_1=ON -DDISABLE_SSE4_2=ON  -DDISABLE_SSE4_A=ON
else
CMAKE_ARCH_FLAGS = -DMRPT_AUTODETECT_SSE=OFF -DDISABLE_SSE2=ON -DDISABLE_SSE3=ON -DDISABLE_SSE4_1=ON -DDISABLE_SSE4_2=ON  -DDISABLE_SSE4_A=ON
endif

# Unit tests can be run with target "test", or "test_gdb", which shows stack
# traces on failure:
MRPT_TEST_TARGET = test_legacy

CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX="$(CURDIR)/debian/usr/" \
	-DCMAKE_INSTALL_LIBDIR="lib/$(DEB_HOST_MULTIARCH)" \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE=Release  \
	-DCMAKE_MRPT_USE_DEB_POSTFIXS="1" \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
	-DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
	-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DCMAKE_EXE_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	$(CMAKE_ARCH_FLAGS) \
	REPLACE_HERE_EXTRA_CMAKE_PARAMS

# Don't set: CMAKE_SKIP_RPATH=ON --> it makes unit tests to fail,
# and the rpath is removed anyway by cmake's install target.

%:
	dh $@ --buildsystem=cmake

override_dh_auto_build-arch:
	dh_auto_build -O--buildsystem=cmake

	# Python bindings:
	dh_python2 --package=python-mrpt


override_dh_auto_build-indep:
	# Build doc targets:
	dh_auto_build -O--buildsystem=cmake -- \
		documentation_html \
		man_pages_all \
		documentation_psgz_guides

	# clean useless doxygen files:
	find $(CURDIR)/doc/ -name '*.md5' | xargs -I FIL rm FIL

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_install:
	# Do nothing?
	echo "[override_dh_auto_install]"

override_dh_auto_test-indep:
	echo "[override_dh_auto_test-indep] Do nothing."

override_dh_auto_test-arch:
	# Tests for arch-dependent :
	echo "[override_dh_auto_test-arch]"
	dh_auto_build -O--buildsystem=cmake -- $(MRPT_TEST_TARGET) -j1

override_dh_install:
	# Install:
	dh_auto_build -O--buildsystem=cmake -- install

	# Move all the ".so.x.y" files to the corresponding binary packages:
	# libmrpt-${name}${CMAKE_MRPT_VERSION_NUMBER_MAJOR}.${CMAKE_MRPT_VERSION_NUMBER_MINOR}/usr/...
	VERSION=$$(head version_prefix.txt -n1 | cut -f 1-2 -d . ); tmpfil=$$(mktemp); find $(CURDIR)/debian/ -name "*.so.?.*" > $$tmpfil; cat $$tmpfil | while read line; do f=$$(basename $$line | cut -f 1 -d '.'); echo "Processing so file: $$line, basename: $$f"; dir=$(CURDIR)/debian/$$f$${VERSION}/usr/lib/$(DEB_HOST_MULTIARCH); mkdir -p $$dir; mv -f $$line $$dir; done

	# JLBC Nov 2018: Due to what is explained in MRPT/cmakemodules/DeclareMRPTLibs.cmake, we need to make some "mv" here:

	# Move all the ".so" files to the proper place: the -dev package:
	tmpfil=$$(mktemp); find $(CURDIR)/debian/ -name "*.so" | grep -v 'dev' | grep -v 'pymrpt' > $$tmpfil; cat $$tmpfil | while read line; do f=$$(basename $$line .so); echo "Processing so file: $$line, basename: $f"; mkdir -p $(CURDIR)/debian/$$f-dev/usr/lib/$(DEB_HOST_MULTIARCH); mv -f $$line $(CURDIR)/debian/$$f-dev/usr/lib/$(DEB_HOST_MULTIARCH); done

	# Move cmake modules to the -dev packages:
	tmpfil=$$(mktemp); find $(CURDIR)/debian/usr/share/ -name '*-config.cmake' > $$tmpfil; cat $$tmpfil | while read line; do f=$$(dirname $$line); f=$$(basename $$f); echo "Processing cmake scripts: $$line, module name: $$f"; dir=$(CURDIR)/debian/lib$$f-dev/usr/share/; mkdir -p $$dir; mv -f $(CURDIR)/debian/usr/share/$$f $$dir; done

	# Move headers to -dev directories:
	cp -vr $(CURDIR)/debian/usr/libmrpt-*dev $(CURDIR)/debian/

	# Move python stuff:
	mkdir -p $(CURDIR)/debian/python-mrpt/usr/lib
	mv -v $(CURDIR)/debian/usr/lib/* $(CURDIR)/debian/python-mrpt/usr/lib

	# Move mrpt-doc:
	dir=$(CURDIR)/debian/mrpt-doc/usr/share/doc/mrpt-doc; mkdir -p $$dir; mv -v $(CURDIR)/debian/usr/mrpt-doc/usr/share/doc/mrpt-doc/* $$dir

	# Move mrpt-apps:
	dir=$(CURDIR)/debian/mrpt-apps/usr/; mkdir -p $$dir; cp -vr $(CURDIR)/debian/usr/mrpt-apps/* $$dir/..

	# Move mrpt-common:
	dir=$(CURDIR)/debian/mrpt-common/usr/; mkdir -p $$dir; cp -vr $(CURDIR)/debian/usr/mrpt-common/* $$dir/..

	# Fix Doxygen's embedded JQuery lib:
	rm $(CURDIR)/debian/mrpt-doc/usr/share/doc/mrpt-doc/html/jquery.js || true
	ln -s /usr/share/javascript/jquery/jquery.js $(CURDIR)/debian/mrpt-doc/usr/share/doc/mrpt-doc/html/jquery.js  || true
